﻿using UnityEngine;
using System.Collections;

namespace MiniGameTrash
{
    public class TrashGameManager : MonoBehaviour
    {
        public GUIText timeText, wrongText, correctText;
        public GameObject berhasil, gagal;
        int score;
        public GameObject[] trashList;
        GameObject sampah;
        Transform trash;
        float startTime, distance;
        public int maxTime;
        int restTime, correct, wrong;
        bool isGameStart, isGameOver, isComplete;

        // Use this for initialization
        void Start()
        {
            for (int i = 0; i < 20; i++)
            {
                Vector2 pos = new Vector2(Random.Range(-6f, 6f), Random.Range(-4f, 1.5f));

                Instantiate(trashList[Random.Range(0, trashList.Length)], pos, transform.rotation);
                //Debug.Log(pos);
            }

            maxTime = (int)Time.time + 30;
            isGameOver = false;
            isGameStart = true;
            correct = wrong = 0;
            isComplete = false;
        }

        // Update is called once per frame
        void Update()
        {
            restTime = maxTime - (int)Time.time;
            
            if (isGameStart && !isGameOver)
            {
                CheckGameOver();
                timeText.text = restTime.ToString();
                CheckClick();
            }

            correctText.text = correct.ToString();
            wrongText.text = wrong.ToString();

            if (isGameOver && !isComplete)
            {
                if (correct + wrong == 20 && wrong <= 3)
                {
                    //berhasil.SendMessage("SetText", "Kamu telah berhasil membantu mebersihkan sampah", SendMessageOptions.DontRequireReceiver);
                    berhasil.GetComponent<ResultScript>().description.text = "Kamu telah berhasil membantu mebersihkan \n sampah";
                    Instantiate(berhasil);
                    
                }
                else
                {
                    gagal.GetComponent<ResultScript>().description.text = "Kamu gagal dalam membantu membersihkan sampah";
                    Instantiate(gagal);
                    //gagal.SendMessage("SetText", "Kamu gagal dalam membantu membersihkan sampah", SendMessageOptions.DontRequireReceiver);
                }
                isComplete = true;
            }
        }

        void CheckClick()
        {
            RaycastHit2D click = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);

            if (Input.GetMouseButtonUp(0) && click.collider != null)
            {
                trash = GameObject.Find("Tempat Sampah Organik").transform;
                startTime = Time.time;
                sampah = click.collider.gameObject;
                distance = Vector2.Distance(sampah.transform.position, trash.position);
                //sampah.transform.position = trash.position;
            }

            if (Input.GetMouseButtonUp(1) && click.collider != null)
            {
                trash = GameObject.Find("Tempat Sampah NonOrganik").transform;
                startTime = Time.time;
                sampah = click.collider.gameObject;
                distance = Vector2.Distance(sampah.transform.position, trash.position);
                //sampah.transform.position = trash.position;
            }

            if (sampah != null)
            {
                float progress = ((Time.time - startTime) * 1.5f) / distance;
                sampah.transform.position = Vector2.Lerp(sampah.transform.position, trash.position, progress);
            }
        }

        void CheckGameOver()
        {
            if (restTime <= 0 || (correct + wrong) == 20)
                isGameOver = true;
        }

        void UpdateScore(int score)
        {
            this.score += score;
            if (score > 0)
                correct++;
            else wrong++;
            //Debug.Log(this.score);
        }
    }
}